const app = getApp();
var openid = wx.getStorageSync('openid');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    winHeight:100,//初始页面高度
    tab: 0,//那个tab按钮
    hasMore: true,//是否有更多
    subtitle: '',
    contentList: [],//内容数据
    page: 1,//当前页面
  },
  /**
   * tab的点击切换和滑动切换
   */
  tab_slide: function (e) { //滑动切换tab 
    var that = this;
    that.setData({
      tab: e.detail.current
    });
    that.initData();
  },
  tab_click: function (e) { //点击tab切换
    var that = this;
    if (that.data.tab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        tab: e.target.dataset.current
      })
    }
    that.initData();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.initData();

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getSystemInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.initData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /*初始化列表数据*/
  initData: function () {
    this.showLoading();
    var that = this;
    var url = app.config.serverCfg.domain.news;
    if (that.data.tab == 1) {
      url = app.config.serverCfg.domain.strategy;
    }
    //重置页码为1
    that.setData({
      page: 1,
    });
    wx.request({
      url: url,
      method: 'post',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {
        openid: openid,
        page: that.data.page,
      },
      success: function (res) {
        that.hideLoading();
        that.setData({
          contentList: res.data.data
        })
      }
    });
  },
  /*加载更多*/
  getMore() {
    wx.showToast({
      title: '努力加载中...',
      icon: 'none',
      duration: 2000
    })
    var that = this;
    this.showLoading();
    var hasMore = true;
    var url = app.config.serverCfg.domain.news;
    if (that.data.tab == 1) {
      url = app.config.serverCfg.domain.strategy;
    }
    wx.request({
      url: url,
      method: 'post',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {
        openid: openid,
        page: ++that.data.page,
      },
      success: function (res) {
        if (res.data.data.length == 0) {
          hasMore = false;
        }
        that.data.contentList = that.data.contentList.concat(res.data.data);
        that.hideLoading();
        that.setData({
          contentList: that.data.contentList,
          hasMore: hasMore,
        })
        wx.hideToast({});//隐藏加载提示
      }
    });
  },
  showLoading() {
    wx.showNavigationBarLoading();
    this.setData({
      subtitle: '加载中...',
      loading: true,
    });
  },
  hideLoading: function () {
    wx.hideNavigationBarLoading();
    wx.stopPullDownRefresh();
    this.setData({
      loading: false
    });
  },
  getSystemInfo: function () {
    var self = this
    wx.getSystemInfo({
      success: res => {
        self.setData({
          winHeight: res.screenHeight,
        })
      }
    })
  },
  /**
   * 进入文章详情
   */
  /**进入文章详情 */
  getDetail: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: 'detail/detail?id=' + id,
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', {
        })
      }
    })
  },
})